package com.zjitc.controller;

import com.zjitc.model.AdminUser;
import com.zjitc.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/17 16:36
 * @description:
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    private ModelAndView login(
            @RequestParam(name = "username") String username,
            @RequestParam(name = "password") String password,
            HttpSession session){
        AdminUser adminUser=adminService.login(username,password);
        if (adminUser==null){
            return new ModelAndView("redirect:/admin/login.jsp");
        }else {
            session.setAttribute("adminUser",adminUser);
            return new ModelAndView("redirect:/admin/home.jsp");
        }
    }

    @RequestMapping("/logout")
    private ModelAndView logout(HttpSession session){
      session.removeAttribute("adminUser");
      return new ModelAndView("redirect:/admin/welcome.jsp");
    }

}
